import 'dart:io';

import 'package:flutter/material.dart';
import 'package:hive_flutter/adapters.dart';
import 'package:styled_widget/styled_widget.dart';
import 'package:test2/base/base_page.dart';
import 'package:test2/model/scene.dart';
import 'package:test2/util/PicUtil.dart';
import 'package:test2/util/bus.dart';
import 'package:test2/util/widget.dart';
import 'package:test2/view/scene_add.dart';

class ItemScene extends StatefulWidget {
  const ItemScene({
    super.key,
    required this.data,
    required this.boxName,
    required this.showMode,
  });

  final String boxName;
  final SceneC data;
  final int showMode; // 1为默认，2为位置放大，3为场景放大

  @override
  State<ItemScene> createState() => _ItemSceneState();
}

class _ItemSceneState extends BaseState<ItemScene> {
  @override
  Widget build(BuildContext context) {
    return Opacity(
      opacity: widget.data.done ? 0.4 : 1,
      child: BorderContainer(
        radius: 8,
        bgColor: Colors.white,
        child: IntrinsicHeight(
          child: Row(
            spacing: 8,
            children: [
              Flexible(
                flex: (widget.showMode != 1 ? 1 : 0),
                fit: (widget.showMode != 1 ? FlexFit.tight : FlexFit.loose),
                child: Stack(
                  fit: StackFit.passthrough,
                  children: [
                    SizedBox(
                      height: (widget.showMode != 1 ? null : 120),
                      width: 180,
                      child: Image.file(
                        File(
                          PicUtil.picRootPath +
                              (widget.showMode == 2
                                  ? widget.data.positionPic
                                  : widget.data.scenePic),
                        ),
                        cacheWidth: 2000,
                        fit: BoxFit.cover,
                      ),
                    ),
                    if (widget.data.tag.isNotEmpty && widget.showMode > 1)
                      Positioned(
                        right: 2,
                        bottom: 2,
                        child: BorderContainer(
                          radius: 30,
                          bgColor: Theme.of(context).primaryColorLight,
                          child: Padding(
                            padding: EdgeInsetsGeometry.symmetric(
                              vertical: 2,
                              horizontal: 8,
                            ),
                            child: Text(
                              widget.data.tag,
                              style: TextStyle(fontSize: 12),
                            ),
                          ),
                        ),
                      ),
                  ],
                ),
              ),
              if (widget.showMode == 1)
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(widget.data.tip),
                      Spacer(),
                      if (widget.data.tag.isNotEmpty)
                        BorderContainer(
                          radius: 30,
                          bgColor: Theme.of(context).primaryColorLight,
                          child: Padding(
                            padding: EdgeInsetsGeometry.symmetric(
                              vertical: 2,
                              horizontal: 8,
                            ),
                            child: Text(
                              widget.data.tag,
                              style: TextStyle(fontSize: 12),
                            ),
                          ),
                        ),
                    ],
                  ).padding(vertical: 8),
                ),

              Column(
                crossAxisAlignment: CrossAxisAlignment.end,
                children: [
                  IconButton(
                    onPressed: () {
                      showDialog(
                        context: context,
                        builder: (_) {
                          return AlertDialog(
                            title: Text("确认删除 ${widget.data.tip}?"),
                            actions: <Widget>[
                              TextButton(
                                child: Text("取消"),
                                onPressed: () {
                                  Navigator.of(context).pop(false);
                                },
                              ),
                              TextButton(
                                child: Text(
                                  "确认",
                                  style: TextStyle(color: Colors.grey),
                                ),
                                onPressed: () async {
                                  await widget.data.deleteSelf();
                                  bus.emit(EventType.scene);
                                  Navigator.of(context).pop(true);
                                },
                              ),
                            ],
                          );
                        },
                      );
                    },
                    icon: Icon(Icons.delete),
                  ),
                  Spacer(),
                  Row(
                    children: [
                      IconButton(
                        onPressed: () {
                          startPage(
                            SceneAdd(
                              info: widget.data,
                              boxName: widget.boxName,
                            ),
                          );
                        },
                        icon: Icon(Icons.edit),
                      ),
                      IconButton(
                        onPressed: () {
                          widget.data.done = !widget.data.done;
                          Hive.box<SceneC>(
                            widget.boxName,
                          ).put(widget.data.key, widget.data);
                          bus.emit(EventType.scene);
                        },
                        icon: Icon(Icons.done),
                      ),
                    ],
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}
